package com.situ.orderingsystem.utils;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

public class BeanFactory {
    private static final HashMap<Class<?>,Object> hm = new HashMap<>();
    public static<T> T getBean(Class<T> tClass){
        T t = (T)hm.get(tClass);
        if(t !=null){
            return t;
        }

            try {
                Constructor<T> constructor = tClass.getDeclaredConstructor();
                 Object obj = constructor.newInstance();
                hm.put(tClass,obj);
                return (T) obj;
            } catch (Exception e) {
                throw new RuntimeException(e);

        }

    }
}
